package com.clstu.qqclient.service;

import com.clstu.qqclient.utilies.Utility;
import com.clstu.qqcommon.Message;
import com.clstu.qqcommon.MessageType;
import com.clstu.qqcommon.User;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 该类用于完成验证用户登录和新用户注册等功能
 */
public class UserClientService {
    //因为可能在其他地方用到User的消息,所以做成属性
    private User u ;

    //用于判断是否登录成功,返回 true/false
    public boolean checkUser(String userID, String pwd){
        boolean b = false;
        //构造用于登录的消息对象
        Message message = new Message(null,MessageType.MESSAGE_LOGIN,new User(userID,pwd));
        try {
            //连接服务器               //                    127.0.0.1表示的是自己的主机,因为服务端也是自己的主机
            Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);//

            //发送User对象
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);//把User对象通过对象流发送出去

            //接受Message对象,只在这个时候接受一个对象,此后的所有需要接受的对象都由线程来管理
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message ms = (Message) ois.readObject();

            //根据Message对象判断成功与否
            if(ms.getMesType().equals(MessageType.MESSAGE_LOGIN_SUCCEED)){

                //如果成功,创建一个和服务端保持通信的线程
                ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(socket,userID);
                clientConnectServerThread.start();//启动线程

                //把创建的线程放入管理类便于管理
                ManageClientConnectServerThread.addClientConnectServerThread(userID,clientConnectServerThread);
                b=true;
            }else {
                //如果登录失败,关闭socket
                socket.close();
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return b;
    }

    //向服务器发起一个获取在线用户列表的消息
    public void getOnline(String userID){
        try {
            //构造一个请求列表的消息
            Message message = new Message();
            message.setSender(userID);
            message.setGetter("朕的服务器");
            message.setMesType(MessageType.MESSAGE_GET_ONLINE_LIST);
            //得到该用户的socket对象并且构造一个对象输出流
            Socket socket = ManageClientConnectServerThread.getClientConnectServerThread(userID).getSocket();
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //向服务器发送一个关闭系统的信息
    public void logout(String userID){
        //构造一个请求列表的消息
        Message message = new Message();
        message.setSender(userID);
        message.setGetter("朕的服务器");
        message.setMesType(MessageType.MESSAGE_EXIT);
        //得到该用户的socket对象并且构造一个对象输出流
        Socket socket = ManageClientConnectServerThread.getClientConnectServerThread(userID).getSocket();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //发送普通的聊天消息
    public void sentMessage(String userID){

        System.out.print("请输入聊天对象:");
        String getter  = Utility.readString(10);
        System.out.println();

        ClientConnectServerThread cCST = ManageClientConnectServerThread.getClientConnectServerThread(userID);
        String s= "";//表示消息内容

        //一直循环发信息,知道输出-1结束
        while (!s.equals("-1")) {
            System.out.print(userID+": ");
            s = Utility.readString();
            //构造一个用于聊天的消息
            Message message = new Message(userID, getter, s, null, MessageType.MESSAGE_COMMON_MES);
            cCST.sentMessage(message);
        }
    }

    //群发消息
    public void sentAll(String userID){
        String s= "";//表示消息内容
        ClientConnectServerThread cCST = ManageClientConnectServerThread.getClientConnectServerThread(userID);
        //一直循环发信息,知道输出-1结束
        while (!s.equals("-1")) {
            System.out.print(userID+": ");
            s = Utility.readString();
            //构造一个用于聊天的消息
            Message message = new Message(userID, null, s, null, MessageType.MESSAGE_SENT_ALL);
            cCST.sentMessage(message);
        }
    }

    //发送文件
    public void sentFile(String userID){
        System.out.print("文件发送给:");
        String getter  = Utility.readString(10);
        System.out.println();
        System.out.print("请输入文件路径:");
        String path = Utility.readString();
        System.out.println();
        System.out.print("请输入目标路径:");
        String dest = Utility.readString();
        System.out.println();
        sentFile(userID,getter,path,dest);

    }

    public static void sentFile(String userID,String getter,String path,String dest){
        //读文件到字节数组
        byte[] data = new byte[(int) new File(path).length()];
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            data = new byte[(int) new File(path).length()];
            fileInputStream.read(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //构造对象发送文件
        Message message = new Message(userID, getter, null, null, MessageType.MESSAGE_SENT_FILE, path,dest,data);
        ClientConnectServerThread cCST = ManageClientConnectServerThread.getClientConnectServerThread(userID);
        cCST.sentMessage(message);
    }

    public User getU() {
        return u;
    }
}
